/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Interfata;

import java.awt.Color;

/**
 *
 * @author AlinT
 */
public class GUInterfata extends javax.swing.JFrame {

    /** Creates new form GUInterfata */
    public GUInterfata() {
        initComponents();
    }

    Memento memento;
    String mem1="", mem2="", mem3="", mem4="", mem5="";

    public static void setDerivata(String s){
        derivata.setText(s);
    }
    public String getDerivata(){
        return derivata.getText();
    }

    public static void setValoare(String s){
        valoare.setText(s);
    }
    public String getValoare(){
        return valoare.getText();
    }

    public static void setMesaj(String s){
        mesaj.setText(s);
    }

    public static String getFunctie()
    {
        return functie.getText();
    }
    public static void setFunctie(String s){
        functie.setText(s);
    }

    public static double getIcs()
    {
        return Double.parseDouble(ics.getText());
    }
    public static void setIcs(String s){
        ics.setText(s);
    }

    public static Memento memento(String f, String d, String x, String v, String m) {
		return new Memento(f, d, x, v, m);
	}

	public void restoreMemento(Memento m) {
		m.restoreData();
	}


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        functie = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        ics = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        derivata = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        valoare = new javax.swing.JTextField();
        Calcul = new javax.swing.JButton();
        Grafic = new javax.swing.JButton();
        XML = new javax.swing.JButton();
        mesaj = new javax.swing.JLabel();
        undo = new javax.swing.JButton();
        redo = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("f(x)=");

        jLabel2.setText("x=");

        jLabel3.setText("f`(x)=");

        derivata.setEditable(false);

        jLabel4.setText("Valoare f(x)=");

        valoare.setEditable(false);

        Calcul.setText("Calcul");
        Calcul.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CalculActionPerformed(evt);
            }
        });

        Grafic.setText("Grafic");
        Grafic.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GraficActionPerformed(evt);
            }
        });

        XML.setText("XML");
        XML.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                XMLActionPerformed(evt);
            }
        });

        mesaj.setBackground(new java.awt.Color(255, 255, 204));
        mesaj.setFont(new java.awt.Font("SansSerif", 1, 12));

        undo.setText("Undo");
        undo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                undoActionPerformed(evt);
            }
        });
        undo.setEnabled(false);

        redo.setText("Redo");
        redo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                redoActionPerformed(evt);
            }
        });
        redo.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(39, 39, 39)
                                        .addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addGap(15, 15, 15)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(functie, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(45, 45, 45)
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(ics, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(32, 32, 32))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(derivata, javax.swing.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(valoare, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(mesaj, javax.swing.GroupLayout.PREFERRED_SIZE, 358, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(redo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(XML, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(undo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Grafic, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Calcul, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(functie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(ics, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(derivata, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(Calcul)
                        .addGap(9, 9, 9)
                        .addComponent(Grafic)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(XML)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(valoare, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(undo, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(mesaj, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(redo))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CalculActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CalculActionPerformed
        // TODO add your handling code here:

        String fct = functie.getText();
        String x = ics.getText();
	if (!fct.equals(mem1) || !x.equals(mem2)) {
		memento = GUInterfata.memento(mem1, mem2, mem3, mem4, mem5);
		undo.setEnabled(true);
        
        mesaj.setText("Succes");
        Complet complet = Complet.doJob(fct, x, evt);
	complet.execute();

	mem1 = fct;
	mem2 = x;
        mem3 = derivata.getText();
        mem4 = valoare.getText();
        mem5 = mesaj.getText();
		}

    }//GEN-LAST:event_CalculActionPerformed

    private void XMLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_XMLActionPerformed
        // TODO add your handling code here:
        
        mesaj.setText("Succes");
        String fct=getFunctie(); String x = ics.getText();
        Complet complet = Complet.doJob(fct, x, evt);
	complet.execute();

    }//GEN-LAST:event_XMLActionPerformed

    private void GraficActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GraficActionPerformed
        // TODO add your handling code here:
        Grafic line=new Grafic();
        line.execute();
 
    }//GEN-LAST:event_GraficActionPerformed

    private void undoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_undoActionPerformed
        // TODO add your handling code here:
        restoreMemento(memento);
	undo.setEnabled(false);
        redo.setEnabled(true);
    }//GEN-LAST:event_undoActionPerformed

    private void redoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_redoActionPerformed
        // TODO add your handling code here:
     
        memento = GUInterfata.memento(mem1, mem2, mem3, mem4, mem5);
	restoreMemento(memento);
	redo.setEnabled(false);
    }//GEN-LAST:event_redoActionPerformed


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUInterfata().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Calcul;
    private javax.swing.JButton Grafic;
    private javax.swing.JButton XML;
    private static javax.swing.JTextField derivata;
    private static javax.swing.JTextField functie;
    private static javax.swing.JTextField ics;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    public static javax.swing.JLabel mesaj;
    private javax.swing.JButton redo;
    private javax.swing.JButton undo;
    private static javax.swing.JTextField valoare;
    // End of variables declaration//GEN-END:variables

}
